package com.alibaba.citrus.cr.mn.order.center.facade.function.convert;

import com.alibaba.citrus.cr.common.util.DateUtils;
import com.alibaba.fastjson.JSON;
import com.epoch.app.crforecastsales.bo.ForecastOrderBO;
import com.epoch.app.crforecastsales.bo.ForecastOrderLineBO;
import com.epoch.app.mnordercenter.model.dto.ModifyForecastDTO;
import com.epoch.app.mnordercenter.model.dto.ModifyForecastDetailDTO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

public class ModifyForecastDTOToForecastOrderBOConvertor {
    public static ForecastOrderBO convert(ModifyForecastDTO modifyForecastDTO) {
        ForecastOrderBO forecastOrderBO = new ForecastOrderBO();
        forecastOrderBO.setSourceOrderNo(modifyForecastDTO.getSourceOrderNo());
        forecastOrderBO.setOrderTypeCode(modifyForecastDTO.getSalesOrderType());
        forecastOrderBO.setTransportModeCode(modifyForecastDTO.getDeliveryMethodCode());
        forecastOrderBO.setCustomerExpectTDate(modifyForecastDTO.getExpectedDeliveryDate());
        if(modifyForecastDTO.getVaildBeginDate() != null) {
            forecastOrderBO.setVaildBeginTime(DateUtils.parse(modifyForecastDTO.getVaildBeginDate()));
        }
        forecastOrderBO.setShipToPartyCode(modifyForecastDTO.getShipToPartyCode());
        forecastOrderBO.setSoldToPartyCode(modifyForecastDTO.getCustomerCode());
        forecastOrderBO.setProductGroupCode(modifyForecastDTO.getProductGroupCode());
        forecastOrderBO.setChannelCode(modifyForecastDTO.getBusinessChannel());
        forecastOrderBO.setSalesChannelCode(modifyForecastDTO.getSalesChannelCode());
        forecastOrderBO.setSalesDepartmentCode(modifyForecastDTO.getSalesDepartmentCode());
        forecastOrderBO.setCreateReasonCode(modifyForecastDTO.getOrderReasonCode());
        forecastOrderBO.setProductGroupCode(modifyForecastDTO.getProductGroupCode());
        forecastOrderBO.setShippingWarehouseCode(modifyForecastDTO.getShippingWarehouseCode());
        forecastOrderBO.setDeliveryFactoryCode(modifyForecastDTO.getDeliveryFactoryCode());
        forecastOrderBO.setStockCode(modifyForecastDTO.getStockCode());
        forecastOrderBO.setCustomerPurchaseOrderNo(modifyForecastDTO.getCustomerPurchaseOrderNo());
        forecastOrderBO.setCustomerPurchaseDate(modifyForecastDTO.getCustomerPurchaseDate());
        forecastOrderBO.setRemark(modifyForecastDTO.getNote());
        forecastOrderBO.setSalesOrganizationCode(modifyForecastDTO.getSalesOrganizationCode());
        forecastOrderBO.setSalesOrganizationCode(modifyForecastDTO.getSalesOrganizationCode());
        forecastOrderBO.setSalesChannelCode(modifyForecastDTO.getSalesChannelCode());
        forecastOrderBO.setSalesDepartmentCode(modifyForecastDTO.getSalesDepartmentCode());
        forecastOrderBO.setModifierCode(modifyForecastDTO.getModifierCode());
//        forecastOrderBO.setModifierName(modifyForecastDTO.getModifierName());
        forecastOrderBO.setOrderBizId(modifyForecastDTO.getSalesOrderNo());
        forecastOrderBO.setBizFeatures(modifyForecastDTO.getBizFeatures());
        forecastOrderBO.setOrderCategoryCode(modifyForecastDTO.getOrderCategoryCode());
        forecastOrderBO.setFulfillmentTypeCode(modifyForecastDTO.getFulfillmentTypeCode());
        forecastOrderBO.setFulfillmentPriority(modifyForecastDTO.getFulfillmentPriority());
        forecastOrderBO.setAutoFulfillment(modifyForecastDTO.getAutoFulfillment());
        if(CollectionUtils.isNotEmpty(modifyForecastDTO.getOrderBizTag())) {
            forecastOrderBO.setOrderTag(String.join(",", modifyForecastDTO.getOrderBizTag()));
        }
        forecastOrderBO.setOrderCategoryCode(modifyForecastDTO.getOrderCategoryCode());
        if (StringUtils.isNotBlank(modifyForecastDTO.getFeatures())) {
            forecastOrderBO.setFeatures(JSON.parseObject(modifyForecastDTO.getFeatures()));
        }
        return forecastOrderBO;
    }

    public static ForecastOrderLineBO lineToBO(ModifyForecastDetailDTO modifyForecastDetailDTO){
        ForecastOrderLineBO forecastOrderLineBO=new ForecastOrderLineBO();
        forecastOrderLineBO.setProductCode(modifyForecastDetailDTO.getProductCode());
        forecastOrderLineBO.setQuantity(modifyForecastDetailDTO.getProductQuantity());
        forecastOrderLineBO.setSaleUnitCode(modifyForecastDetailDTO.getSaleUnitCode());
        forecastOrderLineBO.setDetailRemark(modifyForecastDetailDTO.getNote());
        forecastOrderLineBO.setActivityCode(modifyForecastDetailDTO.getActivityId());
        forecastOrderLineBO.setActivityNumber(modifyForecastDetailDTO.getActivityLineNo());
        forecastOrderLineBO.setShippingWarehouseCode(modifyForecastDetailDTO.getShippingWarehouseCode());
        forecastOrderLineBO.setDeliveryFactoryCode(modifyForecastDetailDTO.getDeliveryFactoryCode());
        forecastOrderLineBO.setStockCode(modifyForecastDetailDTO.getStockCode());
        forecastOrderLineBO.setSourceLineNum(modifyForecastDetailDTO.getSourceLineNum());
        forecastOrderLineBO.setLineNum(modifyForecastDetailDTO.getLineNum());
        forecastOrderLineBO.setBizFeatures(modifyForecastDetailDTO.getBizFeatures());
        forecastOrderLineBO.setGoodsProductationTime(modifyForecastDetailDTO.getProductionDate());
        forecastOrderLineBO.setSourceUnit(modifyForecastDetailDTO.getSourceUnit());
        forecastOrderLineBO.setSourceQuantity(StringUtils.isNotBlank(modifyForecastDetailDTO.getSourceQuantity()) ? Integer.valueOf(modifyForecastDetailDTO.getSourceQuantity()) : null);
        forecastOrderLineBO.setSourcePrice(StringUtils.isBlank(modifyForecastDetailDTO.getSourcePrice()) ? null : new BigDecimal(modifyForecastDetailDTO.getSourcePrice()));
        return forecastOrderLineBO;

    }
}
